package com.way.designpatterncodepratice.prototype;

/**
 * @author liangliwei 80551025
 * @description
 * @date 2020-06-22
 */
public class SheepDeepClone implements Cloneable {
    private String name;
    private int age;
    private String color;
    private String address;
    private SheepDeepClone friend;

    public SheepDeepClone getFriend() {
        return friend;
    }

    public void setFriend(SheepDeepClone friend) {
        this.friend = friend;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "SheepDeepClone{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                ", address='" + address + '\'' +
                ", friend=" + friend +
                '}';
    }

    public SheepDeepClone(String name, int age, String color, String address) {
        this.name = name;
        this.age = age;
        this.color = color;
        this.address = address;
    }

    public SheepDeepClone() {
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        SheepDeepClone sheepDeepClone = new SheepDeepClone();
        sheepDeepClone.setAddress(this.getAddress());
        sheepDeepClone.setAge(this.getAge());
        sheepDeepClone.setName(this.getName());
        sheepDeepClone.setColor(this.getColor());
        /**
         * 这里this.getFriend().clone() 去clone friend 也是Sheep 相当于递归调用
         * 注意friend的friend可能为空 所以要加上一个非空判断
         */
        if (this.getFriend() != null) {
            sheepDeepClone.setFriend((SheepDeepClone) this.getFriend().clone());
        }
        return sheepDeepClone;
    }
}
